#pragma once

#include <QtWidgets/QWidget>
#include <QtWidgets/QVBoxLayout>
#include <QTimer>
#include <QColor>
#include "bondchart.h"
#include "bondcharttools.h"
#include <widgets/chart/basechart_axis.h>
#include <widgets/chart/basechart_layoutitem.h>
#include <qbprotocol/include/SSQBAModel.h>

namespace qb
{
	typedef struct _tag_BONDRATE {
		double	expire;
		double	price;
		const bool operator < (const _tag_BONDRATE& obj) const
		{
			return (expire < obj.expire);
		}
	}BONDRATE, * PBONDRATE;

	typedef struct _tag_BONDTRADEINFO {
		double	expire;			//剩余期限，单位年
		double	price;			//成交价格
		char	sprice[12];
		int		brokerid;		//经纪商ID
		char	code[64];		//债券代码
		char	key[64];		//债券KEY
		char	shortname[64];	//债券简称
		char	remain[16];		//剩余期限
		char	rating[12];		//主体评级
		char	operate[5];		//交易类型，Token，Given，Trade
		time_t	moditytime;		//更新时间
		_tag_BONDTRADEINFO() {
			memset(this, 0, sizeof(_tag_BONDTRADEINFO));
		}
		const bool operator < (const _tag_BONDTRADEINFO& obj) const
		{
			return (moditytime > obj.moditytime);
		}
	}BONDTRADEINFO, * PBONDTRADEINFO;

	class CBondTradeInfoList {
	public:
		std::list<BONDTRADEINFO> m_List;
	public:
		CBondTradeInfoList() {};
		~CBondTradeInfoList() {
			m_List.clear();
		};
	};

    class BondRateChart
		: public QWidget
		, public BaseChartCustomAxisGrid
		, public BaseChartCustomTextElement
    {
		Q_OBJECT
    public:
		BondRateChart(QWidget* parent = nullptr);
		~BondRateChart();

		enum emtype {
			type_All = 0,//完整曲线
			type_Distribute//成交分布
		};

		void setBondInfo(const CBondInfo* bondinfo);
		void loadData(CAnalysisYieldCurve* pYieldCurve, int remain);
		void loadMktStreamDealData(xQBMarketStreamList_c* pData);
		void setType(emtype type);
		void createChart();
		void refreshData();
		void setLeftDrag(double ratio);
		void setRightDrag(double ratio);
		void setCentralDrag(double ratio1, double ratio2);

		BaseChartDragBarElement* dragBar() { return bcde; }

		// BaseChartCustomAxisGrid
		virtual bool drawGridXLines(BaseChartPainter* painter);
		virtual bool drawGridYLines(BaseChartPainter* painter);

		// BaseChartCustomTextElement
		virtual bool customDraw(BaseChartPainter* painter, const QRect& rect);
	signals:
		void mouseDoubleClick(QWidget*);
	protected:
		void drawMouseMoveLine(BaseChartPainter* painter);
		void inputData(CAnalysisYieldCurve* pYieldCurve);
		void clearCurrentCurve();
		void GetShowLimit(double fMinXShow, double fMaxXShow, double& fMinYShow, double& fMaxYShow);
		void DecodeMarketStreamUnit(MarketStreamInfo& stInfo, const xQBMarketStreamUnit_c& unit);
		int GetDate(const char* cDate);
		double getXPos_Value(double value);
		void StartTimer();
		void StopTimer();

		void paintEvent(QPaintEvent* event) override;
		void leaveEvent(QEvent* event) override;

		Q_SLOT void onMouseMove(QMouseEvent* event);

	public slots:
		void onTimer();

	private:
		BaseChartCustomPlot*		customPlot		= nullptr;
		QVBoxLayout*				verticalLayout	= nullptr;
		BaseChartTextElement*		bcte			= nullptr;
		BaseChartDragBarElement*	bcde			= nullptr;
		QTimer*						mTimer			= nullptr;

		CBondInfo*					mBondInfo		= nullptr;
		emtype						mType;
		CAnalysisYieldCurve			m_chartDistData;
		int							m_lRemain;//剩余期限
		std::list<BONDRATE>			m_CurveShow;
		std::list<BONDTRADEINFO>	m_StreamShow;
		QPointF*					m_pLineP		= nullptr;
		int							m_nLinePNum;
		QPointF*					m_pPoints		= nullptr;
		int							m_nPointNum;
		int							m_nHotPoint;
		int							m_nBegin;
		int							m_nEnd;
		int							m_nDealBegin;
		int							m_nDealEnd;
		double						m_dMinX;
		double						m_dMaxX;
		double						m_dMinY;
		double						m_dMaxY;
		double						m_dOriginMinX;
		double						m_dOriginMaxX;
		double						m_dDragLeft;
		double						m_dDragRight;
		double						m_dDealDragLeft;
		double						m_dDealDragRight;
		QColor						m_clrInHour, m_clrOutHour;
    };
}

